package vn.com.vega.music.view.custom;

import vn.com.vega.chacha.R;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class TextBox extends FrameLayout {
	private EditText mEditText;
	private ImageButton mBtnCleanText;
	private Context mContext;

	public TextBox(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public TextBox(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public TextBox(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	private void init(Context ctx) {
		mContext = ctx;
		LayoutInflater inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.textbox, this);
		mEditText = (EditText) findViewById(R.id.textbox_edittext);
		mBtnCleanText = (ImageButton) findViewById(R.id.textbox_button);
		mBtnCleanText.setOnClickListener(onCleanClickListener);
		mEditText.addTextChangedListener(onTextChangeListener);
	}

	View.OnClickListener onCleanClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mEditText.setText("");
			mBtnCleanText.setVisibility(GONE);
		}
	};

	TextWatcher onTextChangeListener = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			if (s.length() > 0) {
				mBtnCleanText.setVisibility(VISIBLE);
			} else {
				mBtnCleanText.setVisibility(GONE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	public void setOnKeyListener(OnKeyListener listener) {
		mEditText.setOnKeyListener(listener);
	}

	public String getText() {
		return mEditText.getText().toString();
	}

	public void setText(String text) {
		mEditText.setText(text);
	}

	public void setTextHint(String textHint) {
		mEditText.setHint(textHint);
	}

	public void setTextHintColor(String colorCode) {
		mEditText.setHintTextColor(Color.parseColor(colorCode));
	}

	public void setTextColor(String colorCode) {
		mEditText.setTextColor(Color.parseColor(colorCode));
	}

	public void setIsPassword(boolean isPass) {
		if (isPass) {
			mEditText.setInputType(InputType.TYPE_CLASS_TEXT
					| InputType.TYPE_TEXT_VARIATION_PASSWORD);
		}
	}

	public void setIsSearchBox(boolean isSearchBox) {
		if (isSearchBox) {
			mEditText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
			mEditText.setOnEditorActionListener(new OnEditorActionListener() {

				@Override
				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					if (event != null
							&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
						InputMethodManager in = (InputMethodManager) mContext
								.getSystemService(Context.INPUT_METHOD_SERVICE);
						in.hideSoftInputFromWindow(
								mEditText.getApplicationWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
						// userValidateEntry();
						// Must return true here to consume event
						return true;

					}
					return false;
				}
			});
		}
	}

	public void setBackground(int ResouceId) {
		mEditText.setBackgroundResource(ResouceId);
	}

	public void setInputTypePhoneNumber() {
		mEditText.setInputType(InputType.TYPE_CLASS_PHONE);
	}
}
